import { loadAreaReq } from '@/services/common'
import { loadEmployees, loadAdmins, loadRoles, loadPositions, loadDepartments } from '@/services/employee'

const state = {
  areaSourceList: [],
  citySourceList: [],
  areaList: [],
  cityList: [],
  provinceArrayList: [],
  cityArrayList: [],
  areaArrayList: [],
  companyEmployeeList: [],
  companyAdminList: [],
  companyRoleList: [],
  companyPositionList: [],
  companyDepartmentList: [],
  areaMap: {}
}

const mutations = {
  updateAreaData: (state, payload) => {
    // const areList = payload
    // const cityList = payload.filter(item => item.level <= 2)
    // state.provinceArrayList = payload.filter(item => item.level === 1)
    // state.cityArrayList = payload.filter(item => item.level === 2)
    // state.areaArrayList = payload.filter(item => item.level === 3)
    // state.areaSourceList = areList
    // state.citySourceList = cityList
    // state.areaList = buildAreaTree(areList)
    // state.cityList = buildAreaTree(cityList)
    const temp = {};
    for (let i = 0; i < payload.length; i++) {
      const item = payload[i];
      temp[item["id"]] = item;
    }
    state.areaMap = temp;
  },
  updateEmployees(state, payload){
    state.companyEmployeeList = payload
  },
  updateAdmins(state, payload){
    state.companyAdminList = payload
  },
  updateRoles(state, payload){
    state.companyRoleList = payload
  },
  updatePositions(state, payload){
    state.companyPositionList = payload
  },
  updateDepartments(state, payload){
    state.companyDepartmentList = payload
  }
}
const actions = {
  loadAreasAct: ({ commit }) => {
    loadAreaReq().then(res => {
      commit('updateAreaData', res)
    }).catch(err => {
      console.log('debug log --> ', err)
    })
  },
  loadEmployeesAct: ({ commit }) => {
    loadEmployees().then(res => {
      commit('updateEmployees', res)
    }).catch(err => {
      console.log('debug log --> ', err)
    })
  },
  loadAdminsAct: ({ commit }) => {
    loadAdmins().then(res => {
      commit('updateAdmins', res)
    }).catch(err => {
      console.log('debug log --> ', err)
    })
  },
  loadRolesAct: ({ commit }) => {
    loadRoles().then(res => {
      commit('updateRoles', res)
    }).catch(err => {
      console.log('debug log --> ', err)
    })
  },
  loadPositionsAct: ({ commit }) => {
    loadPositions().then(res => {
      commit('updatePositions', res)
    }).catch(err => {
      console.log('debug log --> ', err)
    })
  },
  loadDepartmentsAct: ({ commit }) => {
    loadDepartments().then(res => {
      commit('updateDepartments', res)
    }).catch(err => {
      console.log('debug log --> ', err)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

function buildDepPosTree(data, key) {
  return data.map(item => {
    const obj = {
      label: item.name,
      value: item.id,
      id: item.id
    }
    if (item[key]) {
      obj['children'] = buildDepPosTree(item[key], key)
    }
    return obj
  })
}

function buildAreaTree(data) {
  const tree = transAreaArrToTree(data, 0)
  return tree
}

function transAreaArrToTree(data, parentId) {
  const result = []
  let temp = null
  for (let i = 0; i < data.length; i++) {
    if (data[i].parentId === parentId) {
      const obj = Object.assign({}, data[i], {
        label: data[i]['name'],
        value: data[i]['id']
      })
      temp = transAreaArrToTree(data, data[i].id)
      if (temp.length > 0) {
        obj.children = temp
      }
      result.push(obj)
    }
  }
  return result
}
